package com.javasm.service.goodsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goodsCriteria.GdBrandCriteria;
import com.javasm.domin.entity.goodsEntity.GdBrand;
import com.javasm.domin.entity.goodsEntity.GdProduct;
import com.javasm.domin.vo.goodsVo.GdBrandVo;
import com.javasm.mapper.goodsMapper.GdBrandMapper;
import com.javasm.mapper.goodsMapper.GdProductMapper;
import com.javasm.service.goodsService.GdBrandService;
import com.javasm.transfer.goodsTransfer.GdBrandTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author Administrator
* @description 针对表【gd_brand】的数据库操作Service实现
* @createDate 2021-11-19 10:13:08
*/
@Service
@Transactional
public class GdBrandServiceImpl extends ServiceImpl<GdBrandMapper, GdBrand>
implements GdBrandService {

    @Autowired
    private GdBrandMapper gdBrandMapper;

    @Autowired
    private GdProductMapper gdProductMapper;


    @Autowired
    private GdBrandTransfer gdBrandTransfer;

    @Override
    public PageResult<GdBrandVo> selectCriteria(GdBrandCriteria gdBrandCriteria) {
        String brandName = gdBrandCriteria.getBrandName();

        LambdaQueryWrapper<GdBrand> lambda = new QueryWrapper<GdBrand>().lambda();

        if (!StringUtils.isEmpty(brandName)){
            lambda.like(GdBrand::getBrandName,brandName);
        }

        PageHelper.startPage(gdBrandCriteria.getCurrentPage(),gdBrandCriteria.getPageSize());

        List<GdBrand> gdBrandList = gdBrandMapper.selectList(lambda);

        PageInfo<GdBrand> gdBrandPageInfo = new PageInfo<>(gdBrandList);

        long total = gdBrandPageInfo.getTotal();

        List<GdBrandVo> gdBrandVos = gdBrandTransfer.toVO(gdBrandList);


        return new PageResult<GdBrandVo>(total,gdBrandVos);
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {

        List<Long> iii = null;

        int i1 =0;

        for (int i = 0; i < ids.size(); i++) {
            Long aLong = ids.get(i);
            i1 += selectBrandId(aLong);

        }

        if (i1>0){
            return gdBrandMapper.deleteBatchIds(iii);

        }else {
            return gdBrandMapper.deleteBatchIds(ids);
        }


    }

    @Override
    public int selectBrandId(Long id) {

        List<GdProduct> gdProducts = gdProductMapper.selectBrandIdGdProducts(id);

        if (gdProducts.size()>0){

            return 1;

        }else {
            return 0;
        }



    }
}
